package com.gnerv.sylvanas.business.manage.operation;

import com.gnerv.sylvanas.framework.core.BaseCurrentAccount;
import com.gnerv.sylvanas.business.manage.model.bo.DictBO;
import com.gnerv.sylvanas.business.manage.model.entity.Dict;
import com.gnerv.sylvanas.business.manage.model.vo.DictDetailsVO;
import com.gnerv.sylvanas.business.manage.model.vo.DictVO;
import com.gnerv.sylvanas.business.manage.storage.DictItemStorageService;
import com.gnerv.sylvanas.business.manage.storage.DictStorageService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.gnerv.sylvanas.framework.common.tools.PageTools.limitSize;
import static com.gnerv.sylvanas.framework.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Service
@AllArgsConstructor
public class DictOperationService {

    private final DictStorageService dictStorageService;
    private final DictItemStorageService dictItemStorageService;
    private final BaseCurrentAccount baseCurrentAccount;

    
    @Transactional(rollbackFor = {Exception.class})
    public boolean create(DictBO dictBO) {
        Dict dict = dictBO.toDict();
        
        dictStorageService.insert(dict);
        return true;
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByUkId(String ukId) {
        dictStorageService.deleteByUkId(ukId);
        dictItemStorageService.deleteByDictUkId(ukId);
        return true;
    }

    @Transactional(rollbackFor = {Exception.class})
    public boolean updateByUkId(DictBO dictBO) {
        Dict dict = dictBO.toDict();
        dictStorageService.updateByUkId(dict);
        return true;
    }

    @Transactional(readOnly = true)
    public DictVO selectVOByUkId(String ukId) {
        return dictStorageService.selectVOByUkId(ukId);
    }

    public DictDetailsVO selectDetailsVOByUkId(String ukId) {
        return dictStorageService.selectDetailsVOByUkId(ukId);
    }

    public DictDetailsVO selectDetailsVOByCode(String code) {
        return dictStorageService.selectDetailsVOByCode(code);
    }

    @Transactional(readOnly = true)
    public List<DictVO> selectListByName(String name) {
        return dictStorageService.selectListVOByName(name);
    }

    public List<DictDetailsVO> selectListDetailsVOByName(String name) {
        return dictStorageService.selectListDetailsVOByName(name);
    }

    @Transactional(readOnly = true)
    public boolean isExist(String code) {
        return dictStorageService.selectCountByCode(code) > 0;
    }

    public List<DictVO> selectListVOByName(String name, int page, int size) {
        return dictStorageService.selectListVOByName(name, limitStart(page, size), limitSize(size));
    }

    public Map<String, DictDetailsVO> selectMapVOByCode(List<String> codeList) {
        List<DictDetailsVO> dictDetailsVDOList = dictStorageService.selectListDetailsVOByCode(codeList);
        return dictDetailsVDOList.stream().collect(Collectors.toMap(DictDetailsVO::getCode, item -> item));
    }

    public int selectCountByName(String name) {
        return dictStorageService.selectCountByName(name);
    }
}




